package com.lifo.order.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lifo.good.bean.model.HfGoods;
import com.lifo.good.service.SearchGoodsService;
import com.lifo.order.bean.HfOrder;
import com.lifo.order.service.HfOrderService;
import com.lifo.shoppingcart.bean.HfShoppingCart;
import com.lifo.shoppingcart.service.ShoppingCartManagerService;
import com.lifo.utils.common.messagehandler.JsonStandardMsgBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class HfOrderController {
    @Autowired
    private HfOrderService hfOrderService;

    @Autowired
    private ShoppingCartManagerService shoppingCartManagerService;

    @Autowired
    private SearchGoodsService searchGoodsService;



    @Autowired
    private HttpSession session;

    @RequestMapping("/user/order")
    public String getUserOrder(Integer page,Model model){
     /*   page = page == null ? 1 : page;

        final Integer END_INDEX = 20;

        Integer startIndex = ( page -1 ) * END_INDEX;

        Integer userId = (int) session.getAttribute("userId");
        if(!StringUtils.isEmpty(userId)){
            PageInfo<HfOrder> hfOrders = hfOrderService.searchUserOrder(userId, startIndex, END_INDEX);
            //如果分页不为空，且行数大于0就返回true，否则返回FALSE;
            boolean result = (!StringUtils.isEmpty(hfOrders)&&!StringUtils.isEmpty(hfOrders.getRows())&&hfOrders.getRows().size()>0)?true:false;
            if(result){
                model.addAttribute("page",hfOrders);
                model.addAttribute("hfOrderes",hfOrders.getRows());
            }

        }*/
        return "/center/order";
    }


    @RequestMapping("/user/saveOrder")
    @ResponseBody
    public String saveOrder(HttpServletRequest request , HttpServletResponse response){
        Integer userId = (int) session.getAttribute("userId");
        log.debug("========>");
        String goodsIds =  request.getParameter("cartIds");
        String[]  ids = goodsIds.split(",");
        List<HfOrder> list = new ArrayList();
        Long bigOrderId = System.currentTimeMillis();
        for (String id:ids
             ) {
          HfShoppingCart cart =  shoppingCartManagerService.searchHfShoppingCartByCartId(Integer.parseInt(id));

            HfOrder hfOrder = new HfOrder();
            hfOrder.setCount(cart.getCount());
            hfOrder.setColor(cart.getColor());
            hfOrder.setSize(cart.getSize());
            hfOrder.setGoodTitle(cart.getGoodTitle());
            hfOrder.setHfGoodId(cart.getHfGoodId());
            hfOrder.setUserId(userId);
            hfOrder.setBigOrderId(bigOrderId);

            //状态 1未支付 2已支付 3.订单取消
            hfOrder.setStatus(1);
            HfGoods hfGoods= searchGoodsService.searchHfGoodsByHfUid(cart.getHfGoodId());
            hfOrder.setTaobaoGoodId(hfGoods.getTaobaoGoodId());
            hfOrder.setPrice(cart.getTaobaoPrice());
            hfOrder.setTotalMoney(cart.getTaobaoPrice()*cart.getCount());
            list.add(hfOrder);
        }
        hfOrderService.saveUserOrder(list);

        log.debug("================>{}",goodsIds);
        return "订单保存成功";
    }

    @RequestMapping("/user/showOrderDetail")
    public String goOrderDetail(){
        return "/center/orderdetail";
    }

    @RequestMapping(value = "/loadOrder", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public String loadOrder(@RequestBody HfOrder hfOrder){
        log.debug("查询订单参数为==>{}", JSON.toJSONString(hfOrder));
        PageInfo<HfOrder> hfOrderPageInfo = hfOrderService.searchUserOrder(hfOrder.getUserId(),1,15);

        return JsonStandardMsgBean.getMsgJsonWithData(hfOrderPageInfo);
    }
}
